package com.ruoyi.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jl.domain.GameTable;
import com.ruoyi.jl.domain.custome.CustomGameTable;
import com.ruoyi.jl.mapper.GameTableMapper;
import com.ruoyi.jl.service.IGameTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 游戏桌 服务层实现
 *
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Service
public class GameTableServiceImpl extends ServiceImpl<GameTableMapper, GameTable> implements IGameTableService {
    @Autowired
    GameTableMapper tableMapper;

    @Override
    public List<GameTable> list(GameTable gameTable){
        LambdaQueryWrapper<GameTable> wrapper = new LambdaQueryWrapper<>();
        if(gameTable != null){
            if(StringUtils.isNotEmpty(gameTable.getId())){
                wrapper.eq(GameTable::getId, gameTable.getId());
            }
            if(StringUtils.isNotEmpty(gameTable.getBelongGame())){
                wrapper.eq(GameTable::getBelongGame, gameTable.getBelongGame());
            }
            if(StringUtils.isNotEmpty(gameTable.getBelongMultiple())){
                wrapper.eq(GameTable::getBelongMultiple, gameTable.getBelongMultiple());
            }
            if(StringUtils.isNotEmpty(gameTable.getTableNumber())){
                wrapper.eq(GameTable::getTableNumber, gameTable.getTableNumber());
            }
            if(StringUtils.isNotEmpty(gameTable.getPrizePool())){
                wrapper.eq(GameTable::getPrizePool, gameTable.getPrizePool());
            }
            if(StringUtils.isNotEmpty(gameTable.getPrizePoolVariance())){
                wrapper.eq(GameTable::getPrizePoolVariance, gameTable.getPrizePoolVariance());
            }
            if(StringUtils.isNotEmpty(gameTable.getPlayerId())){
                wrapper.eq(GameTable::getPlayerId, gameTable.getPlayerId());
            }
            if(StringUtils.isNotEmpty(gameTable.getPlayerNickname())){
                wrapper.eq(GameTable::getPlayerNickname, gameTable.getPlayerNickname());
            }
            if(StringUtils.isNotEmpty(gameTable.getProfitGold())){
                wrapper.eq(GameTable::getProfitGold, gameTable.getProfitGold());
            }
            if(StringUtils.isNotEmpty(gameTable.getPumpWater())){
                wrapper.eq(GameTable::getPumpWater, gameTable.getPumpWater());
            }
            if(StringUtils.isNotEmpty(gameTable.getStatus())){
                wrapper.eq(GameTable::getStatus, gameTable.getStatus());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<CustomGameTable> tableList(CustomGameTable gameTable) {
        List<CustomGameTable> tables = tableMapper.queryTableList(gameTable);
        return tables;
    }

    @Override
    public CustomGameTable queryById(Integer id) {
        CustomGameTable gameTable = tableMapper.queryById(id);
        return gameTable;
    }

    @Override
    public void updateTableInfo(GameTable gameTable) {
        UpdateWrapper wrapper = new UpdateWrapper();
        if (StringUtils.isNotEmpty(gameTable.getPrizePool())) {
            wrapper.set("prize_pool", gameTable.getPrizePool());
        }
        if (StringUtils.isNotEmpty(gameTable.getPlayerId())) {
            wrapper.set("player_id", gameTable.getPlayerId());
        }
        if (StringUtils.isNotEmpty(gameTable.getPlayerNickname())) {
            wrapper.set("player_nickname", gameTable.getPlayerNickname());
        }
        if (StringUtils.isNotEmpty(gameTable.getStatus())) {
            wrapper.set("status", gameTable.getStatus());
            if(gameTable.getStatus() == 0) {
                wrapper.set("player_id", 0);
            }
        }
        wrapper.eq("id", gameTable.getId());
        this.update(wrapper);
    }
}
